﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSTools.TFSLibrary
{
    public sealed class TFSFieldDefinition : TFSServerEntity
    {
        private FieldDefinition serverFieldDefinition;

        internal TFSFieldDefinition(ITFSConnection connection, FieldDefinition fieldDefinition)
            : base(connection)
        {
            this.serverFieldDefinition = fieldDefinition;
        }

        internal FieldDefinition ServerEntity { get { return this.serverFieldDefinition; } }

        public string Name { get { return this.serverFieldDefinition.Name; } }

        public string ReferenceName { get { return this.serverFieldDefinition.ReferenceName; } }

        public Type SystemType { get { return this.serverFieldDefinition.SystemType; } }

        public override string ToString() { return this.serverFieldDefinition.ToString(); }
    }

    public sealed class TFSFieldDefinitionCollection : TFSServerEntityCollection<TFSFieldDefinition>
    {
        public TFSFieldDefinitionCollection(ITFSConnection connection)
            : base(connection)
        {
        }

        public TFSFieldDefinitionCollection(ITFSConnection connection, IList<TFSFieldDefinition> list)
            : base(connection, list)
        {
        }

        public TFSFieldDefinitionCollection(ITFSConnection connection, int capacity)
            : base(connection, capacity)
        {
        }

        public TFSFieldDefinition this[string name]
        {
            get
            {
                foreach (var field in this)
                {
                    if (field.Name == name)
                    {
                        return field;
                    }
                }

                throw new TFSFieldNotExistException(name);
            }
        }

        internal static TFSFieldDefinitionCollection Create(ITFSConnection connection, FieldDefinitionCollection collection)
        {
            var list = new List<TFSFieldDefinition>(collection.Count);

            foreach (FieldDefinition fieldDefinition in collection)
            {
                list.Add(new TFSFieldDefinition(connection, fieldDefinition));
            }

            return new TFSFieldDefinitionCollection(connection, list);
        }
    }
}
